package com.medical.literature.recognition.test;

import com.medical.literature.recognition.service.DocumentParseService;
import com.medical.literature.recognition.service.KeywordExtractionService;
import com.medical.literature.recognition.service.impl.DocumentParseServiceImpl;
import com.medical.literature.recognition.service.impl.KeywordExtractionServiceImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.util.List;

/**
 * 独立测试模块，用于验证文档识别功能的核心逻辑
 */
public class RecognitionTestModule {
    private static final Logger log = LoggerFactory.getLogger(RecognitionTestModule.class);
    
    private DocumentParseService documentParseService;
    private KeywordExtractionService keywordExtractionService;
    
    public RecognitionTestModule() {
        // 初始化服务
        this.documentParseService = new DocumentParseServiceImpl();
        this.keywordExtractionService = new KeywordExtractionServiceImpl();
    }
    
    /**
     * 测试指定文献文件的识别功能
     * @param filePath 文件路径
     * @param fileType 文件类型
     */
    public void testLiteratureRecognition(String filePath, String fileType) {
        log.info("开始测试文献识别功能，文件路径: {}, 文件类型: {}", filePath, fileType);
        
        try {
            // 1. 测试文件读取和文本提取
            log.info("=== 测试文件读取和文本提取 ===");
            String textContent = documentParseService.extractText(filePath, fileType);
            log.info("文本提取成功，文本长度: {}", textContent.length());
            log.info("文本内容预览: {}", textContent.substring(0, Math.min(200, textContent.length())));
            
            // 2. 测试关键词提取
            log.info("=== 测试关键词提取 ===");
            List<String> keywords = keywordExtractionService.extractKeywords(textContent);
            log.info("关键词提取成功，关键词数量: {}", keywords.size());
            log.info("关键词列表: {}", keywords.subList(0, Math.min(10, keywords.size())));
            
            // 3. 测试医学术语提取
            log.info("=== 测试医学术语提取 ===");
            List<String> medicalTerms = keywordExtractionService.extractMedicalTerms(textContent);
            log.info("医学术语提取成功，术语数量: {}", medicalTerms.size());
            if (!medicalTerms.isEmpty()) {
                log.info("医学术语列表: {}", medicalTerms.subList(0, Math.min(10, medicalTerms.size())));
            }
            
            log.info("文献识别功能测试完成");
            
        } catch (Exception e) {
            log.error("文献识别功能测试失败: {}", e.getMessage(), e);
        }
    }
    
    /**
     * 测试OCR识别功能
     * @param imagePath 图片路径
     */
    public void testOCRRecognition(String imagePath) {
        log.info("开始测试OCR识别功能，图片路径: {}", imagePath);
        
        try {
            String ocrText = documentParseService.recognizeImageText(imagePath);
            log.info("OCR识别成功，文本长度: {}", ocrText.length());
            log.info("OCR文本内容: {}", ocrText.substring(0, Math.min(200, ocrText.length())));
        } catch (Exception e) {
            log.error("OCR识别功能测试失败: {}", e.getMessage(), e);
        }
    }
    
    public static void main(String[] args) {
        RecognitionTestModule testModule = new RecognitionTestModule();
        
        // 测试指定的文献文件（如ID为15的文献）
        // 注意：需要根据实际情况修改文件路径
        String testFilePath = "2025/10/15/2/548fc4b2-c965-4b1d-8551-111dad4e30de.pdf";
        String testFileType = "application/pdf";
        
        testModule.testLiteratureRecognition(testFilePath, testFileType);
        
        // 如果有图片文件需要测试OCR，可以取消下面的注释
        // String testImagePath = "path/to/test/image.png";
        // testModule.testOCRRecognition(testImagePath);
    }
}